//
//  netManager.m
//  tcpoctest
//
//  Created by gump on 10/18/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "netManager.h"
#import "NSString+URLEncoding.h"

#import "client.h"
#import "LogicSB.h"
#import "CheckNetwork.h"
#import "FirCirSendCommentInfo.h"

#import "sendFirCirBgImage.h"
#import "sendFirCirGetBgImage.h"
#import "sendNewsLength.h"
#import "sendFirCirGetID.h"
#import "sendPotocol.h"
#import "sendFirCirGetNews.h"
#import "sendFirCirNewNews.h"
#import "sendFirCirPicture.h"
#import "sendFirCirComment.h"
#import "sendFirCirDelNew.h"

#import "sendOnlineP.h"
#import "sendFirCirGetAllComment.h"

#import "sendLogin.h"
#import "sendLoginBypp.h"
#import "sendconfig.h"
#import "sendQuitLogin.h"

#import "sendGiftListID.h"
#import "sendGiftInfo.h"
#import "sendGiftSend.h"
#import "sendGiftGetStroage.h"
#import "sendGiftListLength.h"

#import "sendnearpeople.h"

#import "sendAddFirend.h"
#import "sendFirendSearch.h"
#import "sendFirendDelete.h"

#import "sendPersonSetName.h"
#import "sendPersonPassWord.h"
#import "sendPersonSetSex.h"
#import "sendPersonSetAge.h"
#import "sendPersonSetPC.h"
#import "sendPersonSetCheck.h"
#import "sendPersonSetSign.h"
#import "sendPersonHeadImage.h"
#import "sendPersonSetMail.h"
#import "sendPersonReadCoin.h"
#import "sendPersonToken.h"
#import "sendPersonDistroble.h"

#import "sendChatText.h"
#import "sendChatImage.h"
#import "sendChatVoice.h"
#import "sendChatRead.h"

#import "sendHeadImageSmall.h"
#import "sendHeadImageBig.h"

#import "sendReadCare.h"
#import "sendReadBeCare.h"

#import "sendSearchPPNum.h"
#import "sendReport.h"

#import "sendRLRichDay.h"
#import "sendRLRichWeek.h"
#import "sendRLRichMonth.h"
#import "sendRLRRichQuarter.h"
#import "sendRLRRichYear.h"
#import "sendRLGlamourDay.h"
#import "sendRLGlamourWeek.h"
#import "sendRLGlamourMonth.h"
#import "sendRLGlamourQuarter.h"
#import "sendRLGlamourYear.h"

#import "sendTaskQuery.h"
#import "sendTaskComplete.h"

#import "PromptLoginDataSource.h"

#import "sendMessageReceipt.h"

#import "LoginViewController.h"

#import "sendTelpGetUser.h"
#import "sendTelepLight.h"
#import "sendTelepCandle.h"

#import "sendqq.h"

#import <pthread.h>
#import <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>



@implementation netManager

static netManager *sharedInstance = nil;
static Client    *c = 0;

+(netManager*)sharedNetManager
{
    if(!sharedInstance)
    {
        sharedInstance = [[super alloc] init];
    }
    return sharedInstance;
}

-(id)init
{
    self = [super init];
    if(self)
    {
        sb = [[LogicSB alloc] init];
        
        self.sendFCBgI = [[sendFirCirBgImage alloc] init];
        [self.sendFCBgI setIsReLogin:true];
        [self.sendFCBgI SetMainIndex:ID_MI_FIRENDCIRCLE
                        withSecIndex:FC_SETBGIMAGE];
        
        self.sendFCGetBGI = [[sendFirCirGetBgImage alloc] init];
        [self.sendFCGetBGI setIsReLogin:true];
        [self.sendFCGetBGI SetMainIndex:ID_MI_FIRENDCIRCLE
                           withSecIndex:FC_GETBGIMAGE];
        
        self.sendFCNewL = [[sendNewsLength alloc] init];
        [self.sendFCNewL setIsReLogin:true];
        [self.sendFCNewL SetMainIndex:ID_MI_FIRENDCIRCLE
                         withSecIndex:FC_GETNEWLENGTH];
        
        self.sendFCGetID = [[sendFirCirGetID alloc] init];
        [self.sendFCGetID setIsReLogin:true];
        [self.sendFCGetID SetMainIndex:ID_MI_FIRENDCIRCLE
                          withSecIndex:FC_GETNEWID];
        
        self.sendp = [[sendPotocol alloc] init];
        [self.sendp setIsReLogin:true];
        
        self.sendFCGetNews = [[sendFirCirGetNews alloc] init];
        [self.sendFCGetNews setIsReLogin:true];
        [self.sendFCGetNews SetMainIndex:ID_MI_FIRENDCIRCLE
                            withSecIndex:FC_ONE];
        
        self.sendFCNewNews = [[sendFirCirNewNews alloc] init];
        [self.sendFCNewNews setIsReLogin:true];
        [self.sendFCNewNews SetMainIndex:ID_MI_FIRENDCIRCLE
                            withSecIndex:FC_SENDNEW];
        
        self.sendFCPic = [[sendFirCirPicture alloc] init];
        [self.sendFCPic setIsReLogin:true];
        [self.sendFCPic SetMainIndex:ID_MI_FIRENDCIRCLE
                        withSecIndex:FC_ADDPICTURE];
        
        self.sendFCComment = [[sendFirCirComment alloc] init];
        [self.sendFCComment setIsReLogin:true];
        [self.sendFCComment SetMainIndex:ID_MI_FIRENDCIRCLE
                            withSecIndex:FC_SENDCOMMENT];
        
        self.sendonline = [[sendOnlineP alloc] init];
        [self.sendonline setIsReLogin:true];
        [self.sendonline SetMainIndex:ID_MI_ONLINE withSecIndex:ID_MI_ONLINE];
        
        self.sendlonginbymail = [[sendLogin alloc] init];
        [self.sendlonginbymail setIsReLogin:false];
        [self.sendlonginbymail SetMainIndex:ID_MI_LOGIN withSecIndex:ID_MI_LOGIN];
        
        self.sendloginpp = [[sendLoginBypp alloc] init];
        [self.sendloginpp setIsReLogin:false];
        [self.sendloginpp SetMainIndex:ID_MI_LOGINWITHPP withSecIndex:ID_MI_LOGINWITHPP];
        
        self.sendc = [[sendconfig alloc] init];
        [self.sendc setIsReLogin:false];
        [self.sendc SetMainIndex:ID_MI_CONFIG withSecIndex:ID_MI_CONFIG];
        
        self.sendquitl = [[sendQuitLogin alloc] init];
        [self.sendquitl setIsReLogin:true];
        [self.sendquitl SetMainIndex:ID_MI_QUITLOGIN withSecIndex:ID_MI_QUITLOGIN];
        
        self.sendFCGetAC = [[sendFirCirGetAllComment alloc] init];
        [self.sendFCGetAC setIsReLogin:true];
        [self.sendFCGetAC SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_ALLCOMMENT];
        
        self.sendgiftLID = [[sendGiftListID alloc] init];
        [self.sendgiftLID setIsReLogin:true];
        [self.sendgiftLID SetMainIndex:ID_MI_GIFT withSecIndex:GI_LIST_ID];
        
        self.sendgiftI = [[sendGiftInfo alloc] init];
        [self.sendgiftI setIsReLogin:true];
        [self.sendgiftI SetMainIndex:ID_MI_GIFT withSecIndex:GI_INFO];
        
        self.sendgiftS = [[sendGiftSend alloc] init];
        [self.sendgiftS setIsReLogin:true];
        [self.sendgiftS SetMainIndex:ID_MI_GIFT withSecIndex:GI_SEND];
        
        self.sendgiftStroage = [[sendGiftGetStroage alloc] init];
        [self.sendgiftStroage setIsReLogin:true];
        [self.sendgiftStroage SetMainIndex:ID_MI_GIFT withSecIndex:GI_GIFT_STORAGE];
        
        self.sendNP = [[sendnearpeople alloc] init];
        [self.sendNP setIsReLogin:true];
        [self.sendNP SetMainIndex:ID_MI_LOCATION withSecIndex:ID_MI_LOCATION];
        
        self.sendFirendA = [[sendAddFirend alloc] init];
        [self.sendFirendA setIsReLogin:true];
        [self.sendFirendA SetMainIndex:ID_MI_ADDFIREND withSecIndex:AFI_SEARCH];
        
        self.sendFirendS = [[sendFirendSearch alloc] init];
        [self.sendFirendS setIsReLogin:true];
        [self.sendFirendS SetMainIndex:ID_MI_ADDFIREND withSecIndex:AFI_SUCESS];
        
        self.sendFirendD = [[sendFirendDelete alloc] init];
        [self.sendFirendD setIsReLogin:true];
        [self.sendFirendD SetMainIndex:ID_MI_ADDFIREND withSecIndex:AFI_SENDDELETECARE];
        
        self.sendPersonSN = [[sendPersonSetName alloc] init];
        [self.sendPersonSN setIsReLogin:true];
        [self.sendPersonSN SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_SETNAME];
        
        self.sendPersonPS = [[sendPersonPassWord alloc] init];
        [self.sendPersonPS setIsReLogin:true];
        [self.sendPersonPS SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_SETPS];
        
        self.sendPersonSS = [[sendPersonSetSex alloc] init];
        [self.sendPersonSS setIsReLogin:true];
        [self.sendPersonSS SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_SETSEX];
        
        self.sendPersonA = [[sendPersonSetAge alloc] init];
        [self.sendPersonA setIsReLogin:true];
        [self.sendPersonA SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_SETAGE];
        
        self.sendPersonSPC = [[sendPersonSetPC alloc] init];
        [self.sendPersonSPC setIsReLogin:true];
        [self.sendPersonSPC SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_SETPC];
        
        self.sendPersonSSign = [[sendPersonSetSign alloc] init];
        [self.sendPersonSSign setIsReLogin:true];
        [self.sendPersonSSign SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_SETSIGN];
        
        self.sendPersonHI = [[sendPersonHeadImage alloc] init];
        [self.sendPersonHI setIsReLogin:true];
        [self.sendPersonHI SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_SETHI];
        
        self.sendPersonSM = [[sendPersonSetMail alloc] init];
        [self.sendPersonSM setIsReLogin:true];
        [self.sendPersonSM SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_SETMAIL];
        
        self.sendPersonRC = [[sendPersonReadCoin alloc] init];
        [self.sendPersonRC setIsReLogin:true];
        [self.sendPersonRC SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_READCOIN];
        
        self.sendCT = [[sendChatText alloc] init];
        [self.sendCT setIsReLogin:true];
        [self.sendCT SetMainIndex:ID_MI_CHAT withSecIndex:CT_TEXT];
        
        self.sendCI = [[sendChatImage alloc] init];
        [self.sendCI setIsReLogin:true];
        [self.sendCI SetMainIndex:ID_MI_CHAT withSecIndex:CT_IMAGE];
        
        self.sendCV = [[sendChatVoice alloc] init];
        [self.sendCV setIsReLogin:true];
        [self.sendCV SetMainIndex:ID_MI_CHAT withSecIndex:CT_VOICE];
        
        self.sendHISmall = [[sendHeadImageSmall alloc] init];
        [self.sendHISmall setIsReLogin:true];
        [self.sendHISmall SetMainIndex:ID_MI_HEADIMAGE withSecIndex:ID_MI_HEADIMAGE];
        
        self.sendHIBig = [[sendHeadImageBig alloc] init];
        [self.sendHIBig setIsReLogin:true];
        [self.sendHIBig SetMainIndex:ID_MI_HEADIMAGE withSecIndex:HII_BIG];
        
        self.sendRC = [[sendReadCare alloc] init];
        [self.sendRC setIsReLogin:true];
        [self.sendRC SetMainIndex:ID_MI_ADDFIREND withSecIndex:AFI_READCAREINFO];
        
        self.sendRBC = [[sendReadBeCare alloc] init];
        [self.sendRBC setIsReLogin:true];
        [self.sendRBC SetMainIndex:ID_MI_ADDFIREND withSecIndex:AFI_READBECAREINFO];
        
        self.sendSPP = [[sendSearchPPNum alloc] init];
        [self.sendSPP setIsReLogin:true];
        [self.sendSPP SetMainIndex:ID_MI_ADDFIREND withSecIndex:AFI_CHECKSUCCESS];
        
        self.sendR = [[sendReport alloc] init];
        [self.sendR setIsReLogin:true];
        [self.sendR SetMainIndex:ID_MI_REPORT withSecIndex:ID_MI_REPORT];
        
        self.sendPersonSC = [[sendPersonSetCheck alloc] init];
        [self.sendPersonSC setIsReLogin:true];
        [self.sendPersonSC SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_SETCHECK];
        
        self.sendgiftLL = [[sendGiftListLength alloc] init];
        [self.sendgiftLL setIsReLogin:true];
        [self.sendgiftLL SetMainIndex:ID_MI_GIFT withSecIndex:GI_LISTLENGTH];
        
        self.sendRLRDay = [[sendRLRichDay alloc] init];
        [self.sendRLRDay setIsReLogin:true];
        [self.sendRLRDay SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_RICH_DAY];
        
        self.sendRLRWeek = [[sendRLRichWeek alloc] init];
        [self.sendRLRWeek setIsReLogin:true];
        [self.sendRLRWeek SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_RICH_WEEK];
        
        self.sendRLRMonth = [[sendRLRichMonth alloc] init];
        [self.sendRLRMonth setIsReLogin:true];
        [self.sendRLRMonth SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_RICH_MONTH];
        
        
        self.sendRLRQuarter = [[sendRLRRichQuarter alloc] init];
        [self.sendRLRQuarter setIsReLogin:true];
        [self.sendRLRQuarter SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_RICH_QUARTER];
        
        self.sendRLRYear = [[sendRLRRichYear alloc] init];
        [self.sendRLRYear setIsReLogin:true];
        [self.sendRLRYear SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_RICH_YEAR];
        
        self.sendRLGDay = [[sendRLGlamourDay alloc] init];
        [self.sendRLGDay setIsReLogin:true];
        [self.sendRLGDay SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_GLAMOUR_DAY];
        
        self.sendRLGWeek = [[sendRLGlamourWeek alloc] init];
        [self.sendRLGWeek setIsReLogin:true];
        [self.sendRLGWeek SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_GLAMOUR_WEEK];
        
        self.sendRLGMonth = [[sendRLGlamourMonth alloc] init];
        [self.sendRLGMonth setIsReLogin:true];
        [self.sendRLGMonth SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_GLAMOUR_MONTH];
        
        self.sendRLGQuarter = [[sendRLGlamourQuarter alloc] init];
        [self.sendRLGQuarter setIsReLogin:true];
        [self.sendRLGQuarter SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_GLAMOUR_QUARTER];
        
        self.sendRLGYear = [[sendRLGlamourYear alloc] init];
        [self.sendRLGYear setIsReLogin:true];
        [self.sendRLGYear SetMainIndex:ID_MI_RANKLIST withSecIndex:RLI_GLAMOUR_YEAR];
        
        self.sendTaskQ = [[sendTaskQuery alloc] init];
        [self.sendTaskQ setIsReLogin:true];
        [self.sendTaskQ SetMainIndex:ID_MI_TASK withSecIndex:TASK_QUERY];
        
        self.sendTaskC = [[sendTaskComplete alloc] init];
        [self.sendTaskC setIsReLogin:true];
        [self.sendTaskC SetMainIndex:ID_MI_TASK withSecIndex:TASK_COMPLETE];
        
        self.sendMsgReceipt = [[sendMessageReceipt alloc] init];
        [self.sendMsgReceipt setIsReLogin:true];
        [self.sendMsgReceipt SetMainIndex:ID_MI_MSGRECEIPT withSecIndex:ID_MI_MSGRECEIPT];
        
        self.sendPersonT = [[sendPersonToken alloc] init];
        [self.sendPersonT setIsReLogin:true];
        [self.sendPersonT SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_TOKEN];
        
        self.sendPersonDisTro = [[sendPersonDistroble alloc] init];
        [self.sendPersonDisTro setIsReLogin:true];
        [self.sendPersonDisTro SetMainIndex:ID_MI_PERSONINFO withSecIndex:PI_DISTRO];
        
        self.sendFCDelNew = [[sendFirCirDelNew alloc] init];
        [self.sendFCDelNew setIsReLogin:true];
        [self.sendFCDelNew SetMainIndex:ID_MI_FIRENDCIRCLE withSecIndex:FC_DELNEW];
        
        self.sendCR = [[sendChatRead alloc] init];
        [self.sendCR setIsReLogin:true];
        [self.sendCR SetMainIndex:ID_MI_CHAT withSecIndex:CT_SENDREAD];
        
        self.sendTeleGetUser = [[sendTelpGetUser alloc] init];
        [self.sendTeleGetUser setIsReLogin:true];
        [self.sendTeleGetUser SetMainIndex:ID_MI_TELEPATHY withSecIndex:TELEP_S_GETUSER];
        
        self.sendTelepL = [[sendTelepLight alloc] init];
        [self.sendTelepL setIsReLogin:true];
        [self.sendTelepL SetMainIndex:ID_MI_TELEPATHY withSecIndex:TELEP_S_LIGHT];
        
        self.sendTelepC = [[sendTelepCandle alloc] init];
        [self.sendTelepC setIsReLogin:true];
        [self.sendTelepC SetMainIndex:ID_MI_TELEPATHY withSecIndex:TELEP_S_CANDLE];
        
        self.sendQQConfig = [[sendqq alloc] init];
        [self.sendQQConfig setIsReLogin:false];
        [self.sendQQConfig SetMainIndex:ID_MI_CONFIG withSecIndex:CONFIGINDEX_QQ_CONFIG_S];
    }
    
    return self;
}

-(void)creatConnect
{
    if (c == 0) {
        c = [[Client alloc]init];
    }
    
    struct hostent* h = NULL;
    @try {
        h = gethostbyname2(IP_IDN,AF_INET);
        
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception);
    }
    
    
    if(h == NULL)
    {
        
        @try {
            h = gethostbyname2(IP_IDN,AF_INET6);
            
        }
        @catch (NSException *exception) {
            NSLog(@"%@",exception);
        }
        
    }
    
    struct sockaddr_in serv_addr;
    if(h != NULL && h->h_addr_list[0] != NULL)
    {
        serv_addr.sin_addr = *((struct in_addr *)h->h_addr_list[0]);
        char *ip = inet_ntoa(serv_addr.sin_addr);
        [c Connect:ip withPort:9527];
        //[c Connect:IP_IP withPort:9527];
    }
}

-(void)closeConnect
{
    [c SetCloseSocket:true];
}

-(void)ReConnect
{
    [c reConnect];
}

- (PACKET *)recvPacket
{
    PACKET *p = [c getPopPacket];
    return p;
}

-(void)Package:(char *)date withLength:(int)len
{
    if ([self isConnect])
    {
        if ([c IsCloseSocket]) {
            [c SetIsReConnect:true];
            [c reConnect];
        }
        else
        {
            [c Package:date withLength:len];
        }
    }
}

-(void)SendHeartBeat
{
    [c PackageHeartBeat];
}

#pragma mark -public

-(bool)isConnect
{
    bool bconnct = true;
    
    if ([CheckNetwork isExistenceNetwork] == NO)
    {
        NS_LOG(@"net no");
        
        [[DataControl shareControl].UICommendDS addUICommand:UC_WARNINGNET];
        
        bconnct = false;
    }
    
    return bconnct;
}

-(void)SendQuitDirectly
{
    if (![DataControl shareControl].blogin) {
        return;
    }
    
    int length = CHAR_PL * 2 + INT_PL;
    
    char *data = (char*)malloc(length);
    assert(data != nil);
    memset(data, 0, length);
    
    [sb initSB:data withLength:length];
    
    [sb WriteInt:getLocalSequence() withNTT:NTT_SMALL2BIG];
    
    [sb WriteChar:ID_MI_QUITLOGIN];
    [sb WriteChar:ID_MI_QUITLOGIN];
    
    [c SendQuitDirectly:data];
    
    [sb cleanSB];
    
    NS_LOG(@"send SendQuitDirectly length %d",length);
}

-(void)reloginBySend
{
    [[DataControl shareControl].promptloginDS SetIsShowMain:false];
    [[DataControl shareControl].promptloginDS SetIsPromptLogin:false];
    [[DataControl shareControl].promptloginDS SetIsInitData:false];
    
    [DataControl shareControl].personDS.uinfo.mail = getLocalMail();
    
    NSString *strppnum = [NSString stringWithFormat:@"%d",
                          getLocalPP()];
    
    if (![strppnum isEqualToString:@"-1"]) {
        [[ViewManager sharedViewManager].loginvc SetMail:strppnum];
    }
    
    if (![[DataControl shareControl].personDS.uinfo.mail isEqualToString:@""])
    {
        if (![[DataControl shareControl].personDS.uinfo.ps isEqualToString:@""])
        {
            [[DataControl shareControl] SetLoginType:LT_MAIL];
            
            [[netManager sharedNetManager].sendlonginbymail sendLoginByMail:
             [DataControl shareControl].personDS.uinfo.mail
                                                                     withPS:
             [DataControl shareControl].personDS.uinfo.ps];
        }
    }
}

@end
